package com.anolesoft.amljmes.perm.service.userMenu.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.anolesoft.amljmes.perm.dto.userMenu.UiResourceDO;
import com.anolesoft.amljmes.perm.dto.userMenu.UiResourceDTO;
import com.anolesoft.amljmes.perm.service.userMenu.inf.UiResourceQueryService;
import com.anolesoft.amljmes.perm.service.userMenu.mapper.UiResourceQueryMapper;
import com.anolesoft.common.security.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhj
 * @date 2022/1/19 10:14
 */
@Service
public class UiResourceQueryServiceImpl implements UiResourceQueryService {
    @Resource
    private UiResourceQueryMapper uiResourceQueryMapper;

//    @Autowired
//    public void setUiResourceQueryMapper(UiResourceQueryMapper uiResourceQueryMapper) {
//        this.uiResourceQueryMapper = uiResourceQueryMapper;
//    }

    /**
     * 有顺序号ORDER_NO
     *
     * @return
     */
    @Override
    public List<UiResourceDTO> selectResources() {
        return this.uiResourceQueryMapper.selectList()
                .stream()
                .map(t -> BeanUtil.copyProperties(t, UiResourceDTO.class))
                .collect(Collectors.toList());
    }

    /**
     * 查询父主键为null
     *
     * @return
     */
    @Override
    public List<UiResourceDTO> selectResourceTree() {
        String permissions = SecurityUtils.getLoginUser().getEmpUid();
        //List<UiResourceDO> dos = this.uiResourceQueryMapper.selectListTree(permissions);
        List<UiResourceDO> dos = this.uiResourceQueryMapper.selectListTree();
        List<UiResourceDTO> dtos = new ArrayList<>(10);
        IterableDtos(dtos, dos);
        return dtos;
    }

    /**
     * 通过当前登录人主键查询当前登录人的菜单
     *
     * @param empUid
     * @return
     */
    @Override
    public List<String> selectResourcesByEmpUid(String empUid) {
        List<String> list = uiResourceQueryMapper.selectResourceByEmpUid(empUid);
        return list;
    }

    private void IterableDtos(List<UiResourceDTO> dtos, List<UiResourceDO> dos) {
        for (UiResourceDO aDo : dos) {
            UiResourceDTO uiResourceDTO = BeanUtil.copyProperties(aDo, UiResourceDTO.class);
            dtos.add(uiResourceDTO);

            List<UiResourceDO> children = aDo.getChildren();

            if (CollectionUtil.isNotEmpty(children)) {
                IterableDtos(dtos, children);
            }

        }
    }


}
